#!/usr/bin/perl -w
#   Written by Jimmy Saw - 10-11-2007
#   This program takes a Genbank file and extract rRNA and tRNA information 
#   You have to redirect output to a file
#   perl parse-gb2rna.pl x.gbk $molid > y.txt    

use Bio::SeqIO;

my $seqio_obj = Bio::SeqIO->new (-file => $ARGV[0], -format => 'genbank');
my $seq_obj = $seqio_obj->next_seq;

for my $feat_obj ($seq_obj->get_SeqFeatures){
    if ($feat_obj->primary_tag eq "rRNA"){

        if ($feat_obj->has_tag('locus_tag')){
            for my $value ($feat_obj->get_tag_values('locus_tag')){
                my $start = $feat_obj->start;
                my $stop = $feat_obj->end;
                my $strand = $feat_obj->strand;
                my $def = $feat_obj->get_tag_values('product');
                my $outseq = $feat_obj->seq->seq();
                print ">", $value, "\t";
            }
        }
        if ($feat_obj->has_tag('product')){
            for my $def ($feat_obj->get_tag_values('product')){
                #my $def = $feat_obj->get_tag_values('product');
                print $def, "\n";
            }
        }
        my $outseq = $feat_obj->seq->seq();
        print $outseq, "\n";
    }
    
    if ($feat_obj->primary_tag eq "tRNA"){

        if ($feat_obj->has_tag('locus_tag')){
            for my $value ($feat_obj->get_tag_values('locus_tag')){
                my $start = $feat_obj->start;
                my $stop = $feat_obj->end;
                my $strand = $feat_obj->strand;
                my $def = $feat_obj->get_tag_values('product');
                my $outseq = $feat_obj->seq->seq();
                print ">", $value, "\t";
            }
        }
        if ($feat_obj->has_tag('product')){
            for my $def ($feat_obj->get_tag_values('product')){
                #my $def = $feat_obj->get_tag_values('product');
                print $def, "\n";
            }
        }
        my $outseq = $feat_obj->seq->seq();
        print $outseq, "\n";
    }
}
